package com.zy.struts.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {

	private List<File> upload;
	private List<String> uploadFileName;
	private List<String> uploadContentType;

	

	public String upload() {
		if (upload.size() > 0) {
			try {
				for (int i=0 ;i<upload.size();i++) {
					FileInputStream in = new FileInputStream(upload.get(i));
					byte[] buffer = new byte[1024];
					String path = ServletActionContext.getRequest()
							.getRealPath("upload");
					System.out.println("path"+path);
					System.out.println(this.uploadContentType);
					String realPath=path+"/"+this.getUploadFileName().get(i);
					OutputStream out = new FileOutputStream(realPath);
					
					int length = 0;
					while ((length = in.read(buffer)) > 0) {
						out.write(buffer, 0, length);
					}

					out.close();
					in.close();

				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return SUCCESS;
		} else
			return "input";
	}



	public List<File> getUpload() {
		return upload;
	}



	public void setUpload(List<File> upload) {
		this.upload = upload;
	}



	public List<String> getUploadFileName() {
		return uploadFileName;
	}



	public void setUploadFileName(List<String> uploadFileName) {
		this.uploadFileName = uploadFileName;
	}



	public List<String> getUploadContentType() {
		return uploadContentType;
	}



	public void setUploadContentType(List<String> uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
}
